package com.sgb.goods.test;

import android.util.Base64;
import android.view.View;
import android.widget.CheckBox;

import androidx.databinding.ObservableBoolean;
import androidx.databinding.ObservableField;

import com.alibaba.android.arouter.launcher.ARouter;
import com.sgb.goods.api.GoodsCallBack;
import com.sgb.goods.test.api.TestNet;
import com.sgb.goods.utils.Constants;
import com.sgb.goods.utils.base.viewmodel.BaseViewModel;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.util.MLog;
import com.swgk.core.util.SharedPreferenceUtil;

import java.util.List;

/**
 * MainViewModel
 *
 * @author cyj
 */
public class MainViewModel extends BaseViewModel {

    public CompanyAdapter adapter;
    /**
     * 用户名
     */
    public ObservableField<String> userName = new ObservableField<>("");
    /**
     * 密码
     */
    public ObservableField<String> userPwd = new ObservableField<>("");
    /**
     * 是否展示登录视图
     */
    public ObservableBoolean isShowLoginView = new ObservableBoolean(true);
    /**
     * 是否展示主页视图
     */
    public ObservableBoolean isShowHomeView = new ObservableBoolean(true);
    /**
     * 是否记住密码选择
     */
    public ObservableBoolean isRememberMeChoose = new ObservableBoolean(true);

    public MainViewModel() {
        adapter = new CompanyAdapter(application.getApplicationContext());
        userName.set(SharedPreferenceUtil.getInstance().getString("goods_user_name_1", ""));
        userPwd.set(SharedPreferenceUtil.getInstance().getString("goods_user_pwd_1", ""));
        isRememberMeChoose.set(SharedPreferenceUtil.getInstance().getBoolean("goods_user_remember_me_1", false));
    }

    /**
     * 记住账户与密码
     */
    public void rememberMeForOnclick(View view) {
        if (((CheckBox) view).isChecked()) {
            SharedPreferenceUtil.getInstance().saveData("goods_user_name_1", userName.get());
            SharedPreferenceUtil.getInstance().saveData("goods_user_pwd_1", userPwd.get());
        }
        SharedPreferenceUtil.getInstance().saveData("goods_user_remember_me_1", ((CheckBox) view).isChecked());
    }

    /**
     * 登录
     */
    public void login() {
        SharedPreferenceUtil.getInstance().saveData(SharedPreferenceUtil.resourcePCToken, "");
        showLoading();
        TestNet.getInstance().getTestApi().login(userName.get(), Base64.encodeToString(userPwd.get().getBytes(), Base64.DEFAULT))
                .enqueue(new GoodsCallBack<BaseEntity<LoginEntity>>() {
                    @Override
                    public void onSuccess(BaseEntity<LoginEntity> data) {
                        MLog.d("login - ", data.getData().toString());
                        SharedPreferenceUtil.getInstance().saveData(SharedPreferenceUtil.resourceAPPToken, data.getData().token);
                        SharedPreferenceUtil.getInstance().saveData(SharedPreferenceUtil.resourcePCToken, data.getData().token);
                        getUserCompany();
                    }
                });
    }

    /**
     * 获取用户加入的所有企业
     */
    public void getUserCompany() {
        showLoading();
        TestNet.getInstance().getTestApi().getUserCompany().enqueue(new GoodsCallBack<BaseEntity<List<CompanyEntity>>>() {
            @Override
            public void onSuccess(BaseEntity<List<CompanyEntity>> data) {
                adapter.setDatas(data.getData());
                isShowLoginView.set(false);
            }
        });
    }

    /**
     * 获设置主企业
     */
    public void settingMainCompany(CompanyEntity entity) {
        showLoading();
        TestNet.getInstance().getTestApi().settingMainCompany(entity.compNo, entity.type).enqueue(new GoodsCallBack<BaseEntity<LoginEntity>>() {
            @Override
            public void onSuccess(BaseEntity<LoginEntity> data) {
                SharedPreferenceUtil.getInstance().saveData(SharedPreferenceUtil.resourcePCToken, data.getData().resourcePcToken);
                SharedPreferenceUtil.getInstance().saveData(SharedPreferenceUtil.COMPANY_NO, entity.compNo);
                // 注意：如果是个人用户登录，那么此处获取的 REAL_ID 将是空值，需要重新获取用户信息接口进行设置；module里不涉及个人登录进行使用。
                SharedPreferenceUtil.getInstance().saveData(SharedPreferenceUtil.REAL_ID, entity.companyId);
                // 此处是首页选择地区时存储的地区码，module用于测试写死为“四川”的地区码
                SharedPreferenceUtil.getInstance().saveString(SharedPreferenceUtil.AREA_CODE, "510000");
                isShowHomeView.set(false);
            }
        });
        getUserInfo();
    }

    private void getUserInfo() {
        showLoading();
        TestNet.getInstance().getTestApi().getUserInfo().enqueue(new GoodsCallBack<BaseEntity<UserMsgEntity>>() {
            @Override
            public void onSuccess(BaseEntity<UserMsgEntity> data) {
                SharedPreferenceUtil.getInstance().saveData(SharedPreferenceUtil.USER_NO, data.getData().getNo());
            }
        });
    }

    /**
     * 跳转到采购订单列表页面
     */
    public void startPurchaseOrderListActivity() {
        ARouter.getInstance().build(Constants.ARouter.AR_PURCHASE_ORDER_LIST).navigation();
    }

    /**
     * 跳转到销售订单列表页面
     */
    public void startSellOrderListActivity() {
        ARouter.getInstance().build(Constants.ARouter.AR_SELL_ORDER_LIST).navigation();
    }

    /**
     * 跳转到我的询价页面
     */
    public void startMyInquiryListActivity() {
        ARouter.getInstance().build(Constants.ARouter.PERSONAL_MY_INQUIRY).navigation();
    }

    /**
     * 跳转到品牌管理页面
     */
    public void startBrandManagerActivity() {
        ARouter.getInstance().build(Constants.ARouter.AR_BRAND_MANAGER).navigation();
    }

    /**
     * 跳转到我的报价页面
     */
    public void startShopMyPriceActivity() {
        ARouter.getInstance().build(Constants.ARouter.AR_SHOP_MY_PRICE).navigation();
    }

    /**
     * 跳转到仓储管理
     */
    public void startStorageManageActivity() {
        ARouter.getInstance().build(Constants.ARouter.AR_STORAGE_MANAGER).navigation();
    }

    /**
     * 跳转到商品发布
     */
    public void startGoodsPublishActivity() {
        ARouter.getInstance().build(Constants.ARouter.AR_GOODS_PUBLISH).navigation();
    }

    /**
     * 跳转到会员价
     */
    public void startVipPriceManageActivity() {
        ARouter.getInstance().build(Constants.ARouter.AR_VIP_PRICE_MANAGE).navigation();
    }

    /**
     * 跳转到商品管理列表
     */
    public void startGoodsManageListActivity() {
        ARouter.getInstance().build(Constants.ARouter.AR_GOODS_MANAGE_LIST).navigation();
    }

    /**
     * 跳转到商品管理列表
     */
    public void startGoodsPublishActivity2() {
        ARouter.getInstance().build(Constants.ARouter.AR_GOODS_PUBLISH_2).navigation();
    }

}
